# Name: Sarjak Maniar
# Email: sm2732@scarletmail.rutgers.edu
# MVA Homework 2

library(readr)
library(ggridges)
library(GGally)
## Loading required package: ggplot2
## Registered S3 method overwritten by 'GGally':
##   method from   
##   +.gg   ggplot2
library(ggvis)
## 
## Attaching package: 'ggvis'
## The following object is masked from 'package:ggplot2':
## 
##     resolution
library(ggthemes)
library(cowplot)
## 
## Attaching package: 'cowplot'
## The following object is masked from 'package:ggthemes':
## 
##     theme_map
library(gapminder)
library(gganimate)
## No renderer backend detected. gganimate will default to writing frames to separate files
## Consider installing:
## - the `gifski` package for gif output
## - the `av` package for video output
## and restarting the R session
## 
## Attaching package: 'gganimate'
## The following object is masked from 'package:ggvis':
## 
##     view_static
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyverse)
## ── Attaching packages
## ───────────────────────────────────────
## tidyverse 1.3.2 ──
## ✔ tibble  3.1.8     ✔ stringr 1.4.1
## ✔ tidyr   1.2.1     ✔ forcats 0.5.2
## ✔ purrr   0.3.4     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter()     masks stats::filter()
## ✖ dplyr::lag()        masks stats::lag()
## ✖ ggvis::resolution() masks ggplot2::resolution()
library(grid)
library(gridExtra)
## 
## Attaching package: 'gridExtra'
## 
## The following object is masked from 'package:dplyr':
## 
##     combine
library(RColorBrewer)

# Bumpus Sparrow Dataset 
Bumpus_sparrows <- read_csv("/Users/sarju/Desktop/MITA Sem 2/MVA/Classwork/Week2/Bumpus_sparrows.csv")
## Rows: 49 Columns: 6
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): Survivorship
## dbl (5): Total_length, Alar_extent, L_beak_head, L_humerous, L_keel_sternum
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Bumpus_sparrows
## # A tibble: 49 × 6
##    Survivorship Total_length Alar_extent L_beak_head L_humerous L_keel_sternum
##    <chr>               <dbl>       <dbl>       <dbl>      <dbl>          <dbl>
##  1 S                     156         245        31.6       18.5           20.5
##  2 S                     154         240        30.4       17.9           19.6
##  3 S                     153         240        31         18.4           20.6
##  4 S                     153         236        30.9       17.7           20.2
##  5 S                     155         243        31.5       18.6           20.3
##  6 S                     163         247        32         19             20.9
##  7 S                     157         238        30.9       18.4           20.2
##  8 S                     155         239        32.8       18.6           21.2
##  9 S                     164         248        32.7       19.1           21.1
## 10 S                     158         238        31         18.8           22  
## # … with 39 more rows
sparrows <- Bumpus_sparrows
sparrows
## # A tibble: 49 × 6
##    Survivorship Total_length Alar_extent L_beak_head L_humerous L_keel_sternum
##    <chr>               <dbl>       <dbl>       <dbl>      <dbl>          <dbl>
##  1 S                     156         245        31.6       18.5           20.5
##  2 S                     154         240        30.4       17.9           19.6
##  3 S                     153         240        31         18.4           20.6
##  4 S                     153         236        30.9       17.7           20.2
##  5 S                     155         243        31.5       18.6           20.3
##  6 S                     163         247        32         19             20.9
##  7 S                     157         238        30.9       18.4           20.2
##  8 S                     155         239        32.8       18.6           21.2
##  9 S                     164         248        32.7       19.1           21.1
## 10 S                     158         238        31         18.8           22  
## # … with 39 more rows
dim(sparrows) # 49  6 [There are 49 rows and 6 columns]
## [1] 49  6
colnames(sparrows) # printing all the column names
## [1] "Survivorship"   "Total_length"   "Alar_extent"    "L_beak_head"   
## [5] "L_humerous"     "L_keel_sternum"
head(sparrows)
## # A tibble: 6 × 6
##   Survivorship Total_length Alar_extent L_beak_head L_humerous L_keel_sternum
##   <chr>               <dbl>       <dbl>       <dbl>      <dbl>          <dbl>
## 1 S                     156         245        31.6       18.5           20.5
## 2 S                     154         240        30.4       17.9           19.6
## 3 S                     153         240        31         18.4           20.6
## 4 S                     153         236        30.9       17.7           20.2
## 5 S                     155         243        31.5       18.6           20.3
## 6 S                     163         247        32         19             20.9
summary(sparrows)
##  Survivorship        Total_length  Alar_extent     L_beak_head   
##  Length:49          Min.   :152   Min.   :230.0   Min.   :30.10  
##  Class :character   1st Qu.:155   1st Qu.:238.0   1st Qu.:30.90  
##  Mode  :character   Median :158   Median :242.0   Median :31.50  
##                     Mean   :158   Mean   :241.3   Mean   :31.46  
##                     3rd Qu.:161   3rd Qu.:245.0   3rd Qu.:32.00  
##                     Max.   :165   Max.   :252.0   Max.   :33.40  
##    L_humerous    L_keel_sternum 
##  Min.   :17.20   Min.   :18.60  
##  1st Qu.:18.10   1st Qu.:20.20  
##  Median :18.50   Median :20.70  
##  Mean   :18.47   Mean   :20.83  
##  3rd Qu.:18.80   3rd Qu.:21.50  
##  Max.   :19.80   Max.   :23.10
str(sparrows) # stars function
## spc_tbl_ [49 × 6] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
##  $ Survivorship  : chr [1:49] "S" "S" "S" "S" ...
##  $ Total_length  : num [1:49] 156 154 153 153 155 163 157 155 164 158 ...
##  $ Alar_extent   : num [1:49] 245 240 240 236 243 247 238 239 248 238 ...
##  $ L_beak_head   : num [1:49] 31.6 30.4 31 30.9 31.5 32 30.9 32.8 32.7 31 ...
##  $ L_humerous    : num [1:49] 18.5 17.9 18.4 17.7 18.6 19 18.4 18.6 19.1 18.8 ...
##  $ L_keel_sternum: num [1:49] 20.5 19.6 20.6 20.2 20.3 20.9 20.2 21.2 21.1 22 ...
##  - attr(*, "spec")=
##   .. cols(
##   ..   Survivorship = col_character(),
##   ..   Total_length = col_double(),
##   ..   Alar_extent = col_double(),
##   ..   L_beak_head = col_double(),
##   ..   L_humerous = col_double(),
##   ..   L_keel_sternum = col_double()
##   .. )
##  - attr(*, "problems")=<externalptr>
boxplot(sparrows[,2:6])

stars(sparrows,labels = sparrows$Survivorship)

# Dive Deeper into Visualization Techniques

attach(sparrows)

#Plots
sparr.birds <- data.frame(as.numeric(rownames(sparrows)),sparrows[,2:6])
sparr.birds
##    as.numeric.rownames.sparrows.. Total_length Alar_extent L_beak_head
## 1                               1          156         245        31.6
## 2                               2          154         240        30.4
## 3                               3          153         240        31.0
## 4                               4          153         236        30.9
## 5                               5          155         243        31.5
## 6                               6          163         247        32.0
## 7                               7          157         238        30.9
## 8                               8          155         239        32.8
## 9                               9          164         248        32.7
## 10                             10          158         238        31.0
## 11                             11          158         240        31.3
## 12                             12          160         244        31.1
## 13                             13          161         246        32.3
## 14                             14          157         245        32.0
## 15                             15          157         235        31.5
## 16                             16          156         237        30.9
## 17                             17          158         244        31.4
## 18                             18          153         238        30.5
## 19                             19          155         236        30.3
## 20                             20          163         246        32.5
## 21                             21          159         236        31.5
## 22                             22          155         240        31.4
## 23                             23          156         240        31.5
## 24                             24          160         242        32.6
## 25                             25          152         232        30.3
## 26                             26          160         250        31.7
## 27                             27          155         237        31.0
## 28                             28          157         245        32.2
## 29                             29          165         245        33.1
## 30                             30          153         231        30.1
## 31                             31          162         239        30.3
## 32                             32          162         243        31.6
## 33                             33          159         245        31.8
## 34                             34          159         247        30.9
## 35                             35          155         243        30.9
## 36                             36          162         252        31.9
## 37                             37          152         230        30.4
## 38                             38          159         242        30.8
## 39                             39          155         238        31.2
## 40                             40          163         249        33.4
## 41                             41          163         242        31.0
## 42                             42          156         237        31.7
## 43                             43          159         238        31.5
## 44                             44          161         245        32.1
## 45                             45          155         235        30.7
## 46                             46          162         247        31.9
## 47                             47          153         237        30.6
## 48                             48          162         245        32.5
## 49                             49          164         248        32.3
##    L_humerous L_keel_sternum
## 1        18.5           20.5
## 2        17.9           19.6
## 3        18.4           20.6
## 4        17.7           20.2
## 5        18.6           20.3
## 6        19.0           20.9
## 7        18.4           20.2
## 8        18.6           21.2
## 9        19.1           21.1
## 10       18.8           22.0
## 11       18.6           22.0
## 12       18.6           20.5
## 13       19.3           21.8
## 14       19.1           20.0
## 15       18.1           19.8
## 16       18.0           20.3
## 17       18.5           21.6
## 18       18.2           20.9
## 19       18.5           20.1
## 20       18.6           21.9
## 21       18.0           21.5
## 22       18.0           20.7
## 23       18.2           20.6
## 24       18.8           21.7
## 25       17.2           19.8
## 26       18.8           22.5
## 27       18.5           20.0
## 28       19.5           21.4
## 29       19.8           22.7
## 30       17.3           19.8
## 31       18.0           23.1
## 32       18.8           21.3
## 33       18.5           21.7
## 34       18.1           19.0
## 35       18.5           21.3
## 36       19.1           22.2
## 37       17.3           18.6
## 38       18.2           20.5
## 39       17.9           19.3
## 40       19.5           22.8
## 41       18.1           20.7
## 42       18.2           20.3
## 43       18.4           20.3
## 44       19.1           20.8
## 45       17.7           19.6
## 46       19.1           20.4
## 47       18.6           20.4
## 48       18.5           21.1
## 49       18.8           20.9
labs.diagonal <- c("Bird","Total length","Alar extent","L. beak & head","L. humerous","L. keel & sternum")
plot(Total_length, Alar_extent,xlab="Total Length (mm)",ylab="Alar extent (nm)",pch=c(16,1))

#Get the Correlations between the measurements
cor(sparrows[-1])
##                Total_length Alar_extent L_beak_head L_humerous L_keel_sternum
## Total_length      1.0000000   0.7349642   0.6618119  0.6452841      0.6051247
## Alar_extent       0.7349642   1.0000000   0.6737411  0.7685087      0.5290138
## L_beak_head       0.6618119   0.6737411   1.0000000  0.7631899      0.5262701
## L_humerous        0.6452841   0.7685087   0.7631899  1.0000000      0.6066493
## L_keel_sternum    0.6051247   0.5290138   0.5262701  0.6066493      1.0000000
# Correlations
pairs(sparrows[,2:6])

# Scatter Plot Matrix
# A scatterplot matrix is a matrix of scatterplots that lets you understand the 
# pairwise relationship between different variables in a dataset.

#create scatterplot matrix (pch=20 means to use a solid circle for points)
plot(sparrows, pch=20, cex=1.5, col='steelblue')

ggpairs(sparrows)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

#Let's do a 3D Scatter Plot

library(scatterplot3d)
s3d <- scatterplot3d(Alar_extent,Total_length,L_beak_head,pch=c(1,16)[as.numeric(Survivorship)],xlab="Alar extent", ylab="", angle=45,zlab="Length of beak and head", lty.hide=2,type="h",y.margin.add=0.1,font.axis=2,font.lab=2)
## Warning in scatterplot3d(Alar_extent, Total_length, L_beak_head, pch = c(1, :
## NAs introduced by coercion
mtext("Total length",side=1, adj=1, line=-4.5, font=2)

#Legend added correct symbol for legend point
legend(s3d$xyz.convert(238, 160, 34.1),c("Non-survivor","Survivor"),pch=c(1,16),text.font=2)

# How about factors? Lattice package helps with this.
library(lattice)
super.sym <- trellis.par.get("superpose.symbol")
super.sym$superpose.symbol$pch <- c(1,16,rep(1,5))
super.sym$superpose.symbol$col <- rep("#000000",7)
trellis.par.set(super.sym)
splom(~sparr.birds, groups = Survivorship, data = sparr.birds, ps=0.5, varname.cex = .5,panel = panel.superpose,key = list(columns = 2,points = list(pch = super.sym$pch[1:2], col=super.sym$col[1:2]),text = list(c("Non-survivor", "Survivor"))))

# GGally package
library(GGally)
ggscatmat(sparrows, columns=2:6, color="Survivorship")
## Warning: The dot-dot notation (`..scaled..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(scaled)` instead.
## ℹ The deprecated feature was likely used in the GGally package.
##   Please report the issue at <]8;;https://github.com/ggobi/ggally/issueshttps://github.com/ggobi/ggally/issues]8;;>.

stars(sparr.birds)

stars(sparr.birds,labels=row.names(sparr.birds))

# Computing Correlation Matrix
corrm.sparrows <- cor(sparrows[-1])
corrm.sparrows
##                Total_length Alar_extent L_beak_head L_humerous L_keel_sternum
## Total_length      1.0000000   0.7349642   0.6618119  0.6452841      0.6051247
## Alar_extent       0.7349642   1.0000000   0.6737411  0.7685087      0.5290138
## L_beak_head       0.6618119   0.6737411   1.0000000  0.7631899      0.5262701
## L_humerous        0.6452841   0.7685087   0.7631899  1.0000000      0.6066493
## L_keel_sternum    0.6051247   0.5290138   0.5262701  0.6066493      1.0000000
plot(corrm.sparrows)

# xyplot

plot(Total_length~Alar_extent)

abline(lm(Total_length~Alar_extent), col="red")

xyplot(Total_length~Alar_extent)

xyplot(Total_length~Alar_extent | Survivorship)

xyplot(Total_length~Alar_extent, groups=Survivorship)

xyplot(Total_length~Alar_extent | Survivorship + L_beak_head)

xyplot(Total_length~Alar_extent  | Survivorship , groups=Survivorship, auto.key=list(space="right"))

library(ggplot2)
# Basic scatter plot
ggplot(sparrows, aes(x=Total_length, y=Alar_extent)) + geom_point()

# Change the point size, and shape
ggplot(sparrows, aes(x=Total_length, y=Alar_extent)) + geom_point(size=2, shape=23)

ggplot(sparrows, aes(x=Total_length,y=Alar_extent)) + facet_wrap(~Survivorship) + geom_point()

# Label points in the scatter plot
# The function geom_text() can be used :

ggplot(sparrows, aes(x=Total_length, y=Alar_extent)) +geom_point() + geom_text(label=rownames(sparrows),position = position_dodge(width = 1),vjust = -1, size = 2)

ggplot(data = sparrows, aes(x=Total_length, y=Alar_extent)) +
  geom_boxplot(fill=c('steelblue'))
## Warning: Continuous x aesthetic
## ℹ did you forget `aes(group = ...)`?

# Add regression lines
# The functions below can be used to add regression lines to a scatter plot :
#   
# geom_smooth() and stat_smooth()
# geom_abline()

geom_smooth(method="auto", se=TRUE, fullrange=FALSE, level=0.95)
## geom_smooth: na.rm = FALSE, orientation = NA, se = TRUE
## stat_smooth: na.rm = FALSE, orientation = NA, se = TRUE, fullrange = FALSE, level = 0.95, method = auto
## position_identity
# Add the regression line
ggplot(sparrows, aes(x=Total_length, y=Alar_extent)) + 
  geom_point()+
  geom_smooth(method=lm)
## `geom_smooth()` using formula = 'y ~ x'

# Remove the confidence interval
ggplot(sparrows, aes(x=Total_length, y=Alar_extent)) + 
  geom_point()+
  geom_smooth(method=lm, se=FALSE)
## `geom_smooth()` using formula = 'y ~ x'

# Loess method
ggplot(sparrows, aes(x=Total_length, y=Alar_extent)) + 
  geom_point()+
  geom_smooth()
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

# Change the appearance of points and lines
#  We are describing how to change :

# 1) the color and the shape of points
# 2) the line type and color of the regression line
# 3) the fill color of the confidence interval

# Change the point colors and shapes
# Change the line type and color

ggplot(sparrows, aes(x=Total_length, y=Alar_extent)) + 
  geom_point(shape=18, color="blue")+
  geom_smooth(method=lm, se=FALSE, linetype="dashed",
              color="darkred")
## `geom_smooth()` using formula = 'y ~ x'

# Change the confidence interval fill color
ggplot(sparrows, aes(x=Total_length, y=Alar_extent)) + 
  geom_point(shape=18, color="blue")+
  geom_smooth(method=lm,  linetype="dashed",
              color="darkred", fill="blue")
## `geom_smooth()` using formula = 'y ~ x'

# Scatter plots with multiple groups
# We try to describe how to change point colors and shapes automatically and manually.
# Change the point color/shape/size automatically

# Change point shapes by the levels of Survivorship
ggplot(sparrows, aes(x=Total_length, y=Alar_extent, shape=Survivorship)) +
  geom_point()

# Change point shapes and colors
ggplot(sparrows, aes(x=Total_length, y=Alar_extent, shape=Survivorship, color=Survivorship)) +
  geom_point()

# Add regression lines
# Regression lines can be added as follow :

# Add regression lines
ggplot(sparrows, aes(x=Total_length, y=Alar_extent, color=Survivorship, shape=Survivorship)) +
  geom_point() + 
  geom_smooth(method=lm)
## `geom_smooth()` using formula = 'y ~ x'

# Remove confidence intervals
# Extend the regression lines
ggplot(sparrows, aes(x=Total_length, y=Alar_extent, color=Survivorship, shape=Survivorship)) +
  geom_point() + 
  geom_smooth(method=lm, se=FALSE, fullrange=TRUE)
## `geom_smooth()` using formula = 'y ~ x'

# The fill color of confidence bands can be changed as follow :
ggplot(sparrows, aes(x=Total_length, y=Alar_extent, color=Survivorship, shape=Survivorship)) +
  geom_point() + 
  geom_smooth(method=lm, aes(fill=Survivorship))
## `geom_smooth()` using formula = 'y ~ x'

# Change point shapes and colors manually
ggplot(sparrows, aes(x=Total_length, y=Alar_extent, color=Survivorship, shape=Survivorship)) +
  geom_point() + 
  geom_smooth(method=lm, se=FALSE, fullrange=TRUE)+
  scale_shape_manual(values=c(3, 16))+ 
  scale_color_manual(values=c('#999999','#E69F00'))+
  theme(legend.position="top")
## `geom_smooth()` using formula = 'y ~ x'

# Change the point sizes manually
ggplot(sparrows, aes(x=Total_length, y=Alar_extent, color=Survivorship, shape=Survivorship))+
  geom_point(aes(size=Survivorship)) + 
  geom_smooth(method=lm, se=FALSE, fullrange=TRUE)+
  scale_shape_manual(values=c(3, 16))+ 
  scale_color_manual(values=c('#999999','#E69F00'))+
  scale_size_manual(values=c(2,3,4))+
  theme(legend.position="top")
## `geom_smooth()` using formula = 'y ~ x'

# Adding marginal rugs to a scatter plot
# The function geom_rug() can be used :

geom_rug(sides ="bl")
## geom_rug: outside = FALSE, sides = bl, length = 0.03, na.rm = FALSE
## stat_identity: na.rm = FALSE
## position_identity
# sides : a string that controls which sides of the plot the rugs appear on. 
# Allowed value is a string containing any of “trbl”, for top, right, bottom, and left.

# Add marginal rugs
ggplot(sparrows, aes(x=Total_length, y=Alar_extent)) +
  geom_point() + geom_rug()

# Change colors
ggplot(sparrows, aes(x=Total_length, y=Alar_extent, color=Survivorship)) +
  geom_point() + geom_rug()

# Scatter plots with the 2d density estimation
# The functions geom_density_2d() or stat_density_2d() can be used :

# Scatter plot with the 2d density estimation
sp <- ggplot(sparrows, aes(x=Total_length, y=Alar_extent)) +
  geom_point()
sp + geom_density_2d()

# Gradient color
sp + stat_density_2d(aes(fill = ..level..), geom="polygon")

# Change the gradient color
sp + stat_density_2d(aes(fill = ..level..), geom="polygon")+
  scale_fill_gradient(low="blue", high="red")

# Scatter plots with ellipses
# The function stat_ellipse() can be used as follow:

# One ellipse arround all points
ggplot(sparrows, aes(Total_length, Alar_extent))+
  geom_point()+
  stat_ellipse()

# Ellipse by groups
p <- ggplot(sparrows, aes(Total_length, Alar_extent, color = Survivorship))+
  geom_point()
p + stat_ellipse()

# Change the type of ellipses: possible values are "t", "norm", "euclid"
p + stat_ellipse(type = "norm")

# bar chart
ggplot(sparr.birds, aes(Survivorship)) + geom_bar(position="stack") 

# ggplot(sparr.birds, aes(clarity)) + facet_grid(.~cut) + geom_bar(position="dodge")

# histogram
ggplot(sparrows, aes(Alar_extent))+geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

ggplot(sparrows, aes(L_beak_head))+geom_histogram(aes(fill = after_stat(count)))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

ggplot(sparrows, aes(L_humerous))+geom_histogram(aes(fill = after_stat(count)))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

# violin plot 
# A violin plot is a hybrid of a box plot and a kernel density plot, which shows 
# peaks in the data. It is used to visualize the distribution of numerical data. 
# Unlike a box plot that can only show summary statistics, violin plots depict 
# summary statistics and the density of each variable.

ggplot(sparrows, aes(x=Total_length, y=Alar_extent)) + geom_violin()

ggplot(sparrows, aes(x=Total_length, y=L_humerous)) + geom_violin()

ggplot(sparrows, aes(x=Total_length, y=L_keel_sternum)) + geom_violin()

# box plot
ggplot(sparrows, aes(x=Total_length, y=Alar_extent)) + geom_boxplot()
## Warning: Continuous x aesthetic
## ℹ did you forget `aes(group = ...)`?

ggplot(sparrows, aes(x=Total_length, y=Alar_extent)) + geom_boxplot() + coord_flip()
## Warning: Continuous x aesthetic
## ℹ did you forget `aes(group = ...)`?

# density plot and ggridges
# A density plot can be seen as an extension of the histogram. As opposed to the histogram, the density plot can smooth out the distribution of values and reduce the noise. It visualizes the distribution of data over a given period, and the peaks show where values are concentrated.

ggplot(sparrows, aes(x=Total_length)) + geom_density() 

ggplot(sparrows, aes(x=Total_length, fill=Alar_extent, color=Survivorship)) + geom_density() 
## Warning: The following aesthetics were dropped during statistical transformation: fill
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
##   the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
##   variable into a factor?

ggplot(sparrows, aes(x=Total_length, fill=L_humerous, color=Survivorship)) + geom_density(alpha=0.3, aes(y=..scaled..)) 
## Warning: The following aesthetics were dropped during statistical transformation: fill
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
##   the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
##   variable into a factor?

ggplot(sparrows, aes(x=Total_length, y=Survivorship)) + geom_density_ridges() 
## Picking joint bandwidth of 1.63

# hexbin
# A hexbin plot is useful to represent the relationship of 2 numerical variables when you have a lot of data points. Without overlapping of the points, the plotting window is split into several hexbins. The color of each hexbin denotes the number of points in it.

ggplot(sparrows, aes(x=Total_length, y=Alar_extent)) + geom_hex() 

ggplot(sparrows, aes(x=Total_length, y=L_humerous)) + geom_hex() 

# with ggthemes (see also ggsci, ggthemr)
lastplot <- ggplot(sparrows, aes(x=Total_length,y=Alar_extent)) + xlim(0,3) + geom_point(aes(color=Survivorship)) + stat_smooth() + 
  labs(x="total length", y="alar extent", title="Bumpus Sparrow Data") 

lastplot + theme_bw()
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 49 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 49 rows containing missing values (`geom_point()`).

lastplot + theme_cowplot()
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 49 rows containing non-finite values (`stat_smooth()`).
## Removed 49 rows containing missing values (`geom_point()`).

lastplot + theme_dark()
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 49 rows containing non-finite values (`stat_smooth()`).
## Removed 49 rows containing missing values (`geom_point()`).

lastplot + theme_economist()
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 49 rows containing non-finite values (`stat_smooth()`).
## Removed 49 rows containing missing values (`geom_point()`).

lastplot + theme_fivethirtyeight()
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 49 rows containing non-finite values (`stat_smooth()`).
## Removed 49 rows containing missing values (`geom_point()`).

lastplot + theme_tufte()
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 49 rows containing non-finite values (`stat_smooth()`).
## Removed 49 rows containing missing values (`geom_point()`).

lastplot + theme_wsj()
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 49 rows containing non-finite values (`stat_smooth()`).
## Removed 49 rows containing missing values (`geom_point()`).

detach(sparrows)